/*  
 * Copyright (c) 2010, James Daniello
 * This project is licensed under the 'New BSD' license.
 * The full text of the license can be found in the root
 * directory under the filename "LICENSE.
"
 */
package src.objects;

import java.util.Scanner;
import java.io.File;
import src.engine.PICAIUAnimation;

/**
 * A gunner unit. Can be either on the red team or the blue team. Code specific to the Gunners will
 * be here, or else any code general to all units will be in the PICAIUGameUnit superclass.
 */
public class PICAIUGameUnit_Gunner extends PICAIUGameUnit implements PICAIUCollidable, PICAIUSelectable {

  //animations
  PICAIUAnimation walking;
  PICAIUAnimation run;
  PICAIUAnimation still;
  PICAIUAnimation goProne;
  PICAIUAnimation unProne;
  PICAIUAnimation shoot;
  PICAIUAnimation shootProne;
  PICAIUAnimation death;

  public PICAIUGameUnit_Gunner(boolean redTeamIn, double xIn, double yIn, long idIn) {

    position.setX(xIn);
    position.setY(yIn);
    width = 24;
    height = 27;
    tMiddle = 22;

    selectable = true;


    setTeam(redTeam = redTeamIn);

    id = idIn;

    //FOR DEBUGGING PURPOSES. LATER WILL BE CALLED FROM DATA FROM SERVER
    //ONE POSSIBILITY IS THAT THE SERVER WILL SEND OVER ONE OF EACH SCRIPT AND THEY
    //WILL BE HOUSED IN THE GAME ENGINE CLASS
    /*String scriptIn ="";
    try{
    Scanner sc = new Scanner(new File("scripts/test.lua"));
    while(sc.hasNextLine()){
    scriptIn+=sc.nextLine();
    }
    loadScript(scriptIn);
    }catch(Exception e){}
     */
    status = REST;
  }

  public void setTeam(boolean redTeam) {

    String color = "red";
    if (!redTeam) {
      color = "blue";
    }
    still = new PICAIUAnimation("gunner_" + color + "_still.png", -1, -1);
    run = new PICAIUAnimation("gunner_" + color + "_run.gif", 8, 60);
    walking = new PICAIUAnimation("gunner_" + color + "_walking.png", 8, 135);
    goProne = new PICAIUAnimation("gunner_" + color + "_goprone.png", 2, 135);
    unProne = new PICAIUAnimation("gunner_" + color + "_unprone.png", 2, 135);
    shoot = new PICAIUAnimation("gunner_" + color + "_shoot.gif", 6, 135);
    death = new PICAIUAnimation("gunner_" + color + "_death.gif", 10, 135);
    shootProne = new PICAIUAnimation("gunner_" + color + "_shootprone.gif", 6, 135);

    if (redTeam) {
      collisionBounds.add(new PICAIURectBounds(4, 16, 18, 10));
      selectionBounds.add(new PICAIURectBounds(6, 0, 16, 27));
      goProne.setOffX(-15);
      unProne.setOffX(-15);
      run.setOffX(2);
      walking.setOffX(-2);
      shoot.setOffX(-12);
      shootProne.setOffX(-12);
    } else {
      collisionBounds.add(new PICAIURectBounds(0, 16, 18, 10));
      selectionBounds.add(new PICAIURectBounds(0, 0, 16, 27));
      goProne.setOffX(-7);
      unProne.setOffX(-7);
      run.setOffX(-2);
      walking.setOffX(2);
      shoot.setOffX(-12);
      xFlip = true;
    }

    animation = still;
    death.setLoop(false);
    goProne.setLoop(false);
    unProne.setLoop(false);
    shoot.setLoop(false);
    shoot.setOffY(1);
    shootProne.setOffY(10);
  }

  /**
   * Returns the animation for this unit when they're moving
   */
  public PICAIUAnimation getMovingAnimation() {
    return walking;
  }

  /**
   * Updates the current object and performs its routines each frame
   */
  @Override
  public void update(long deltaT) {
    if (alive) {
      setAnimation();
      animation.update(deltaT);
      //script.runFunction("run",this,deltaT);
      if (status == RUN || status == MOVE) {
        //moveTowardsTarget(deltaT);
      }
    }

  }

  /**
   * Set's the animation of this unit
   */
  private void setAnimation() {
    if (status == NORMAL) {
      animation = still;
    } else if (status == DEAD) {
      if (animation != death) {
        animation = death;
        animation.restart();
        //alive=false;

      }
    } else if (status == REST) {
      animation = still;
    } else if (status == RUN) {
      animation = run;
    } else if (status == MOVE) {
      animation = walking;
    } else if (status == GOPRONE) {
      animation = goProne;
      animation.restart();
      status = BLOCKED;
    } else if (status == UNPRONE) {
      animation = unProne;
      animation.restart();
      status = BLOCKED;
    } else if (status == SHOOT) {
      animation = shoot;
      animation.restart();
      status = BLOCKED;
    } else if (status == SHOOTPRONE) {
      animation = shootProne;
      animation.restart();
      status = BLOCKED;
    } else if (status == BLOCKED) {
      if (animation == goProne) {
        if (animation.hasFinished()) {
          status = PRONE;
        }
      } else if (animation == unProne) {
        if (animation.hasFinished()) {
          status = NORMAL;
        }
      } else if (animation == shoot) {
        if (animation.hasFinished()) {
          status = NORMAL;
        }
      } else if (animation == shootProne) {
        if (animation.hasFinished()) {
          status = PRONE;
          animation = goProne;
          animation.setFrame(1);
        }
      }
    }

    if (target.getX() > position.getX()) {
      xFlip = true;
    } else if (target.getX() < position.getX()) {
      xFlip = false;
    }
  }

  
  public void shoot() {
    if (status == NORMAL) {
      status = SHOOT;
    } else if (status == PRONE) {
      status = SHOOTPRONE;
    }
    if (status == RUN) {
      status = SHOOT;
    }
  }

  public void goProne() {
    status = GOPRONE;
  }

  public void unProne() {
    status = UNPRONE;
  }

  /**
   *  Returns where the target cursor should be located for this object
   */
  @Override
  public int getTargetLockX() {
    if (redTeam) {
      return (int) position.getX() + 3;
    } else {
      return (int) position.getX() - 3;
    }
  }

  /**
   *  Returns where the target cursor should be located for this object
   */
  @Override
  public int getTargetLockY() {
    if (redTeam) {
      return (int) position.getY() - 5;
    } else {
      return (int) position.getY() - 5;
    }
  }

  /**
   * returns the offset required to line up this object in the selection veiwer
   */
  @Override
  public int getViewerOffsetX() {
    if (redTeam) {
      return 34;
    } else {
      return 38;
    }
  }

  /**
   * returns the offset required to line up this object in the selection veiwer
   */
  @Override
  public int getViewerOffsetY() {
    return 33;
  }
}
